"""
Case Type   : openGauss 支持忽略PLSQL间的依赖进行创建
Case Name   : 存储过程的入参类型存在嵌套依赖关系（存储过程的入参类型依赖自定义type，type依赖表字段)，增加表字段
Create At   : 2023.11.23
Owner       : zhanghuan96
Description :
    1. 使用gs_guc reload方式设置参数为plpgsql_dependency
    2. 创建表及自定义类型
    3. 创建存储过程，存储过程的入参类型依赖自定义type，type依赖表字段
    4. 调用存储过程;增加表字段;再次调用;手动执行重编译语句;再次调用;
    5. 清理环境
Expect      :
    1. 成功
    2. 成功
    3. 成功
    4. 成功;成功;报错;成功;成功
    5. 成功
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class PlpgsqlDependency(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.common = Common()
        self.constant = Constant()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_node = Node('PrimaryDbUser')
        self.tb_name = "tb_guc0016"
        self.type_name = "type_guc0016"
        self.proc_name = "proc_guc0016"
        self.default_value = self.common.show_param('behavior_compat_options')

    def test_guc(self):
        text = '--step1:使用gs_guc reload方式设置参数为plpgsql_dependency;' \
               'expect:成功--'
        self.log.info(text)
        mod_msg = self.pri_sh.execute_gsguc('reload',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            "behavior_compat_options "
                                            "='plpgsql_dependency'")
        self.log.info(mod_msg)
        self.assertTrue(mod_msg)
        sql_cmd = "show behavior_compat_options;"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEqual('plpgsql_dependency',
                         sql_res.splitlines()[-2].strip(),
                         '查询失败')

        text = '--step2:创建表及自定义类型;expect:成功--'
        self.log.info(text)
        sql_cmd = f'''drop table if exists {self.tb_name} cascade;
        create table {self.tb_name}(sno int, name varchar, sex varchar, \
        cno int);
        drop type if exists {self.type_name};
        create type {self.type_name} as (a int, c {self.tb_name}%RowType);'''
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('CREATE TYPE', sql_res, '查询失败')
        self.assertIn('CREATE TABLE', sql_res, '查询失败')

        text = '--step3:创建存储过程，存储过程的入参类型依赖自定义type，type依赖表字段;' \
               'expect:成功--'
        self.log.info(text)
        sql_cmd = f'''create or replace procedure \
        {self.proc_name}(p1 {self.type_name})  as
        begin
        raise info 'p1=:%',p1;
        exception
        when no_data_found then raise info 'no_data_found';
        end;      
        '''
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('CREATE PROCEDURE', sql_res, '查询失败')

        text = '--step4:调用存储过程;增加表字段;再次调用;手动执行重编译语句;再次调用;' \
               'expect:成功;成功;报错;成功;成功--'
        self.log.info(text)
        sql_cmd = f'''call {self.proc_name}((1,(1,'zhang','M',1)));       
        alter table {self.tb_name} ADD column b int;
        call {self.proc_name}((1,(1,'zhang','M',1,2)));
        alter procedure {self.proc_name} compile;
        call {self.proc_name}((1,(1,'zhang','M',1,2)));'''
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('INFO:  p1=:(1,"(1,zhang,M,1)")', sql_res,
                      '查询失败')
        self.assertIn('ALTER TABLE', sql_res, '查询失败')
        self.assertIn(
            'ERROR:  type tb_guc0016 has been changed, please switch '
            'the session', sql_res, '查询失败')
        self.assertIn('ALTER PROCEDURE', sql_res, '查询失败')
        self.assertIn('INFO:  p1=:(1,"(1,zhang,M,1,2)")', sql_res, '查询失败')

    #
    def tearDown(self):
        text = '----step5:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop procedure {self.proc_name};" \
                  f"drop type if exists {self.type_name} cascade;" \
                  f"drop table if exists {self.tb_name};"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        mod_msg = self.pri_sh.execute_gsguc('reload',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            f"behavior_compat_options "
                                            f"='{self.default_value}'")
        self.log.info(mod_msg)
        self.assertTrue(mod_msg)
        self.assertIn('DROP PROCEDURE', sql_res, '删除失败')
        self.assertIn('DROP TYPE', sql_res, '删除失败')
        self.assertIn('DROP TABLE', sql_res, '删除失败')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
